package Stock.StockGUIS;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
import java.text.*;
import java.sql.Connection;
import java.sql.*;
import Stock.StockDAOS.*;


public class StockSearchPanel extends JPanel implements ActionListener
{
	JButton searchStockB=null, addStockB=null, searchB=null;
	JPanel northBtnP=null, baseP=null, searchAreaP=null, baseP2=null, eastP=null;
	JLabel searchLbl=null;
	JTextField searchTxt=null;
	JComboBox searchJcb=null;
	JTextArea jta=null;
	StockDetailsPanel sdp=null;
	StockEditPanel sep=null;
	StockAddNewPanel sanp=null;
	private JDialog stockDialog = null;
	private JButton dialogAddB, dialogCancelB;
	private JTable stockTable = null;
	StockNewManager snm;
	Connection Conn;
	StockNew stock;
	private int accessLevel;
	String queryStockTypeId = "SELECT stockTypeName FROM stockTypeTable WHERE stockTypeId = ?";
	String queryCatNameId =	"SELECT catName FROM priceCatTable WHERE catId = ?";		
			
	public StockSearchPanel(Connection Conn) 
	{
		this.Conn = Conn;
		this.setLayout(new GridLayout(1,1));
		sdp = new StockDetailsPanel(Conn);
		sanp = new StockAddNewPanel(sdp, Conn);
		sep = new StockEditPanel(sdp, Conn);
		
		
		accessLevel = 2;
		
		northBtnP = new JPanel(new GridLayout(1,2));
		searchStockB = new JButton("Search-4-Stock Item");
		searchStockB.setToolTipText("Find Stock Item");
		searchStockB.addActionListener(this);
		searchStockB.setBorderPainted(false);
		
		searchStockB.setFocusPainted(false);
		addStockB = new JButton("Add Stock");
		addStockB.setToolTipText("Add New Stock Item");
		addStockB.addActionListener(this);
		addStockB.setBorderPainted(false);
		
		addStockB.setFocusPainted(false);
		northBtnP.add(searchStockB);
		northBtnP.add(addStockB);
		northBtnP.setPreferredSize(new Dimension(780, 30));
		baseP = new JPanel(new BorderLayout());
		baseP2 = new JPanel(new BorderLayout());
		baseP.add(northBtnP, BorderLayout.NORTH);
		baseP.add(baseP2, BorderLayout.CENTER);
		
		searchAreaP = new JPanel();
		BoxLayout b1 = new BoxLayout(searchAreaP, BoxLayout.X_AXIS);
		searchAreaP.setLayout(b1);
		searchLbl = new JLabel(" Enter Stock Item Title/Barcode/Stock ID ");
		searchTxt = new JTextField();
		searchTxt.addActionListener(this);
		String [] jcbString = new String[]{
				"Title", "Barcode", "Stock ID"};
		searchJcb = new JComboBox(jcbString);
		searchB = new JButton(" Search ");
		searchB.addActionListener(this);
		searchAreaP.add(searchLbl);
		searchAreaP.add(searchTxt);
		searchAreaP.add(searchJcb);
		searchAreaP.add(searchB);
		searchAreaP.setBorder(BorderFactory.createEmptyBorder(5,10,5,10));
		
		eastP = new JPanel();
		BoxLayout b3 = new BoxLayout(eastP, BoxLayout.Y_AXIS);
		eastP.setLayout(b3);
		jta = new JTextArea(null, 15, 28);
		JScrollPane jsp = new JScrollPane(jta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
							JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
							
		eastP.add(jsp);
		
		baseP2.add(searchAreaP, BorderLayout.NORTH);
		baseP2.add(sdp, BorderLayout.WEST);
		baseP2.add(eastP, BorderLayout.EAST);
		baseP2.add(sep, BorderLayout.SOUTH);
	
	
		this.add(baseP);	
		dialogAddB = new JButton("ADD");
		dialogAddB.addActionListener(this);
		dialogCancelB = new JButton("CANCEL");
		dialogCancelB.addActionListener(this);
		snm = new StockNewManager(Conn);		
		addStockB.doClick();
		searchStockB.doClick();
		searchTxt.requestFocus();
		setFieldsSelectable(false);	
		
	}	
	
	//Used to get the access level for this panel
	public int getAccessLevel()
	{
		return accessLevel;
	}
	
	public void setFieldsSelectable(Boolean tf)
	{
		if(tf)
		{
			sdp.stockTypeJcb.setEnabled(true);
			sdp.barcodeTxt.setEnabled(true);
			sdp.brandTxt.setEnabled(true);
			sdp.serialNoTxt.setEnabled(true);
			sdp.descriptionTxt.setEnabled(true);
			sdp.titleTxt.setEnabled(true);
			sdp.catIdJcb.setEnabled(true);
			sdp.runTimeTxt.setEnabled(true);
			sdp.ageRestTxt.setEnabled(true);
			sdp.directorTxt.setEnabled(true);
			sdp.actor1Txt.setEnabled(true);
			sdp.actor2Txt.setEnabled(true);
			sdp.lastJdc.setEnabled(true);
			sdp.releaseDateTxt.setEnabled(true);
			sdp.genreJcb.setEnabled(true);
			sdp.statusJcb.setEnabled(true);
		}
		else if(tf ==  false)
		{
			sdp.mediaStockIdTxt.setEnabled(false);
			sdp.stockTypeJcb.setEnabled(false);
			sdp.barcodeTxt.setEnabled(false);
			sdp.brandTxt.setEnabled(false);
			sdp.serialNoTxt.setEnabled(false);
			sdp.descriptionTxt.setEnabled(false);
			sdp.titleTxt.setEnabled(false);
			sdp.catIdJcb.setEnabled(false);
			sdp.runTimeTxt.setEnabled(false);
			sdp.ageRestTxt.setEnabled(false);
			sdp.directorTxt.setEnabled(false);
			sdp.actor1Txt.setEnabled(false);
			sdp.actor2Txt.setEnabled(false);
			sdp.lastJdc.setEnabled(false);
			sdp.releaseDateTxt.setEnabled(false);
			sdp.genreJcb.setEnabled(false);
			sdp.statusJcb.setEnabled(false);
		}
	}
	
	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		if(source == searchStockB)
		{
			try
			{
				System.out.println("in searchStockB");
				baseP2.removeAll();
				addStockB.setBackground(new Color(215,215,215));
				searchStockB.setBackground(new Color(238,238,238));
				baseP2.add(searchAreaP, BorderLayout.NORTH);
				baseP2.add(sdp, BorderLayout.WEST);
				baseP2.add(eastP, BorderLayout.EAST);
				baseP2.add(sep, BorderLayout.SOUTH);
				baseP2.validate();
				baseP2.repaint();
				searchTxt.requestFocus();
				this.validate();
				this.repaint();
			}
			catch(Exception excep)
			{
				excep.printStackTrace();
			}	
			
		}
		if(source == addStockB)
		{
			try
			{
				System.out.println("in addStockB");
				
				baseP2.removeAll();
				searchStockB.setBackground(new Color(215,215,215));
				addStockB.setBackground(new Color(238,238,238));
				baseP2.add(searchAreaP, BorderLayout.NORTH);
				baseP2.add(sdp, BorderLayout.WEST);
				baseP2.add(eastP, BorderLayout.EAST);
				baseP2.add(sanp, BorderLayout.SOUTH);
				baseP2.validate();
				baseP2.repaint();
				this.validate();
				this.repaint();
				
				//baseP.add(sanp, BorderLayout.SOUTH);
				//sanp.validate();
				//sanp.repaint();
				//baseP.validate();
				//baseP.repaint();
				//this.repaint();
				//this.validate();
				setFieldsSelectable(true);
				sdp.stockTypeJcb.requestFocus();
				System.out.println("in addStockB bottom");
				
				
			}
			catch(Exception excep)
			{
				excep.printStackTrace();
			}	
		}
		if((source == searchB)||(source == searchTxt))
		{
			String criteria = (String)searchJcb.getSelectedItem();
			System.out.println(criteria);
		
			String searchText = searchTxt.getText();
			System.out.println("Text in searchTxt = "+searchText);
			
			if((criteria.equals("Title"))||(criteria.equals("Barcode"))||(criteria.equals("Stock ID")))
			{
				stockTable = new JTable();
				stockTable = snm.searchStock(searchText, criteria);
				if(stockTable.getRowCount() > 1)
				{
					System.out.println("in row table");
					stockDialog = new JDialog();
					stockDialog.setLayout(new BorderLayout());
					JPanel dialogButtonPanel = new JPanel(new GridLayout(1,2));
					dialogButtonPanel.add(dialogAddB);
					dialogButtonPanel.add(dialogCancelB);
					JScrollPane stockJSP = new JScrollPane(stockTable);
					stockJSP.setHorizontalScrollBarPolicy(
						ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
					stockJSP.setVerticalScrollBarPolicy(
						ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
					stockDialog.add(stockJSP, BorderLayout.CENTER);
					stockDialog.add(dialogButtonPanel, BorderLayout.SOUTH);
					stockDialog.setSize(380,250);
					stockDialog.setLocationRelativeTo(null);
					stockDialog.setVisible(true);
					return;
				}
			}
			
			StockNewManager snm = new StockNewManager(Conn);
			StockNew sn = snm.findStock(searchText, criteria);
			String [] temp = sn.getStockString();
			
			for(int i = 0; i < temp.length; i++)
			{
				System.out.println("sn.toString = "+temp[i]);
			}
				//sep = null;
				//baseP.remove(sep);
				
			sep = new StockEditPanel(sdp, sn, Conn);
				//baseP.add(sep, BorderLayout.SOUTH);
				//this.validate();
				//this.repaint();
				
			if(sn == null)
			{
				JOptionPane.showMessageDialog(this, 
					"Couldn't find stock item!", "Error", 
					JOptionPane.ERROR_MESSAGE);
				return;
			}
			String temp1 = sn.getStockTypeId();
			String temp2 = convertStockTypeId(temp1);
			
			String temp4 = sn.getCatId();
			String temp5 = convertCatNameId(temp4);
			
			setFieldsSelectable(true);
			sdp.mediaStockIdTxt.setText(sn.getStockId());
			sdp.stockTypeJcb.setSelectedItem(temp2);
			sdp.titleTxt.setText(sn.getTitle());
			sdp.statusJcb.setSelectedItem(sn.getStatus());
			sdp.barcodeTxt.setText(sn.getBarcode());
			sdp.brandTxt.setText(sn.getBrand());
			sdp.serialNoTxt.setText(sn.getSerialNo());
			sdp.descriptionTxt.setText(sn.getDescription());
			sdp.catIdJcb.setSelectedItem(temp5);
			sdp.runTimeTxt.setText(sn.getRunTime());
			sdp.releaseDateTxt.setText(sn.getReleaseDate());
			sdp.genreJcb.setSelectedItem(sn.getGenre());
			sdp.ageRestTxt.setText(sn.getAgeRest());
			sdp.directorTxt.setText(sn.getDirector());
			sdp.actor1Txt.setText(sn.getActor1());
			sdp.actor2Txt.setText(sn.getActor2());
			try
			{
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				java.util.Date date = sdf.parse(sn.getDateLastRented());
				sdp.lastJdc.setDate(date);
			}
			catch(ParseException pe)
			{
				pe.printStackTrace();
			}
			sdp.lastJdc.setFocusable(false);
			searchTxt.setText("");
			this.repaint();
			this.validate();
		}
			
	
		if(source == dialogAddB)
		{
			try
			{
				int rowIndex = stockTable.getSelectedRow();
				searchTxt.setText((String)stockTable.getValueAt(rowIndex,0));
				searchJcb.setSelectedItem("Stock ID");
				searchB.doClick();
				stockDialog.dispose();
			}
			catch(Exception ex)
			{
				
			}
		}
		if(source == dialogCancelB)
		{
			stockDialog.dispose();
		}
	}
	
	public String convertStockTypeId(String inputString)
	{
		String input = inputString;
		try
		{
			PreparedStatement pstmt = Conn.prepareStatement(queryStockTypeId);
			ResultSet rs;
			pstmt.setString(1, input);
			rs = pstmt.executeQuery();
			rs.next();
			String temp = rs.getString(1);
			return temp;	
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
			return "";
		}
		 
	}
	
	public String convertCatNameId(String inputString)
	{
		String input = inputString;
		try
		{
			PreparedStatement pstmt = Conn.prepareStatement(queryCatNameId);
			ResultSet rs;
			pstmt.setString(1, input);
			rs = pstmt.executeQuery();
			rs.next();
			String temp4 = rs.getString(1);
			return temp4;	
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
			return "";
		}
		 
	}
}
